'''
Author: sky 1575951471@qq.com
Date: 2023-12-30 16:06:37
LastEditors: sky 1575951471@qq.com
LastEditTime: 2023-12-31 16:14:26
FilePath: \python\arxml_python\CreateSSMPort.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import os
import xlrd3
import dhnLib as dl
import xml.etree.ElementTree as ET

def GetFiles():
    workspace = os.getcwd().replace('\\','/') + '/'
    xlsxDir = dl.FindFilePath(workspace, '358-HTCP_SRD_V2.3-20231219.xlsx')
    outpArxmlDir = dl.FindFilePath(workspace, 'Appl_If_Patac_Core0_swc.arxml')
    return xlsxDir, outpArxmlDir

def ExtractSpecInfoLostFromExcel(xlsxDir):

    TotalSigList =[]
    NoneSigGrp_DicList = []
    SigGrp_DicList = []
    string = []
    xlsxBook = xlrd3.open_workbook(xlsxDir)
    #获取NoneSignalGroup 的数据信息
    NoneSigGrp_sheetInfo = xlsxBook.sheet_by_index(2)
    #获取SignalGroup 的数据信息
    SigGrp_sheetInfo = xlsxBook.sheet_by_index(1)
    #获取表格的总行数
    totalRow = NoneSigGrp_sheetInfo.nrows
    #获取表格总列数
    totalcol = NoneSigGrp_sheetInfo.ncols
    print("NoneTable:nrow ",totalRow)
    print("NoneTable:ncol ",totalcol)
    for row in range(totalRow - 1):
        if(NoneSigGrp_sheetInfo.cell_value(row+1, totalcol-2) == 'B1'):
            string = NoneSigGrp_sheetInfo.cell_value(row+1, 3).split('/')
            for char in string:
              #填充非Group类型信号接口到列表中
              NoneSigGrp_DicList.append(NoneSigGrp_sheetInfo.cell_value(row+1, 1) + '_' + char)
    #print(NoneSigGrp_DicList)
    #获取SignalGroup表格的总行数
    totalRow = SigGrp_sheetInfo.nrows
    #获取SignalGroup表格的总列数
    totalcol = SigGrp_sheetInfo.ncols
    print("NoneTable:nrow ",totalRow)
    print("NoneTable:ncol ",totalcol)

    for row in range(totalRow - 1):
        if(SigGrp_sheetInfo.cell_value(row+1, totalcol-2) == 'B1'):
            string = SigGrp_sheetInfo.cell_value(row+1, 3).split('/')
            for char in string:
              #填充非Group类型信号接口到列表中
              SigGrp_DicList.append(SigGrp_sheetInfo.cell_value(row+1, 1) + '_' + char)

    #print(SigGrp_DicList)
    TotalSigList = SigGrp_DicList + NoneSigGrp_DicList
    print("NoneSig:",len(NoneSigGrp_DicList))
    print("SigGrp:",len(SigGrp_DicList))
    print("TotalSig:",len(TotalSigList))
    return TotalSigList

def InsertRPortToAppl(TotalSigList, outpArxmlDir):
    tree = ET.parse(outpArxmlDir)
    root = tree.getroot()
    portsNode = root.find('.//{http://autosar.org/schema/r4.0}PORTS')
    av_port = '''      <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/RxMonitoredSignal_Av</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''
    LcFA_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SSM_LostCommwithProvider</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''
    FcI_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SignalGroup_FcI</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''
    FcFA_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SignalGroup_FcFA</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''    
    FsI_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SignalGroup_FsI</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''
    FsFA_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SignalGroup_FsFA</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''
    FoI_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SignalGroup_FoI</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''
    FoFA_port = '''            <R-PORT-PROTOTYPE>
              <SHORT-NAME>SSM_$SIG$</SHORT-NAME>
              <REQUIRED-INTERFACE-TREF DEST="SENDER-RECEIVER-INTERFACE">/GM_SUM/Interfaces/SignalGroup_FoFA</REQUIRED-INTERFACE-TREF>
            </R-PORT-PROTOTYPE>\n'''

    for Sig in TotalSigList:
        print(Sig)
        if(Sig.split('_')[-1] == 'Av'):
            AvPortStr = av_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(AvPortStr)
            portsNode.append(portNode)
        if(Sig.split('_')[-1] == 'LcFA'):
            LcFAPortStr = LcFA_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(LcFAPortStr)
            portsNode.append(portNode)
        if(Sig.split('_')[-1] == 'FcI'):
            FcIPortStr = FcI_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(FcIPortStr)
            portsNode.append(portNode)
        if(Sig.split('_')[-1] == 'FcFA'):
            FcFAPortStr = FcFA_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(FcFAPortStr)
            portsNode.append(portNode)
        if(Sig.split('_')[-1] == 'FoI'):
            FoIPortStr = FoI_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(FoIPortStr)
            portsNode.append(portNode)
        if(Sig.split('_')[-1] == 'FoFA'):
            FoFAPortStr = FoFA_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(FoFAPortStr)
            portsNode.append(portNode)
        if(Sig.split('_')[-1] == 'FsI'):
            FsIPortStr = FsI_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(FsIPortStr)
            portsNode.append(portNode)            
        if(Sig.split('_')[-1] == 'FsFA'):
            FsFAPortStr = FsFA_port.replace('$SIG$', Sig)
            portNode = ET.fromstring(FsFAPortStr)
            portsNode.append(portNode)
        tmpList = list(portsNode)
        tmpList[-1].tail = '\n            '
            
    dl.WriteArxml(tree, 'TMP.arxml') 
if __name__ == '__main__':
    xlsxDir, outpArxmlDir = GetFiles()
    TotalSigList=ExtractSpecInfoLostFromExcel(xlsxDir)
    InsertRPortToAppl(TotalSigList, outpArxmlDir)